home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Found / FWExcLib / Include / FWExcept.h < prev    next >
Encoding:
Text File  |  1996-08-16  |  5.4 KB  |  159 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWExcept.h
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. //========================================================================================
  11. //    THEORY OF OPERATION
  12. //
  13. //    This subsystem provides an emulation for C++ exception handling for compilers that
  14. //    do not yet support exceptions.
  15. //
  16. //    When an exception is thrown, the stack is unwound to the first enclosing try/catch 
  17. //    block.  Automatic (stack) objects are destroyed during stack unwinding.  Exceptions
  18. //    thrown during the construction of either an automatic or a dynamic (heap) object will
  19. //    result in the fully constructed subobjects of the partially constructed object
  20. //    being destroyed.
  21. //
  22. //    To obtain this behavior with exception handling emulation, the programmer must do
  23. //    extra work.  Classes which are to be destroyed during unwinding must be "registered"
  24. //    as auto-destruct classes, and each constructor and destructor must invoke macros
  25. //    so that the emulation subsystem can track which objects have been fully constructed
  26. //    and when they go out of scope.
  27. //
  28. //========================================================================================
  29.  
  30. #ifndef FWEXCEPT_H
  31. #define FWEXCEPT_H
  32.  
  33. #ifndef FW_NATIVE_EXCEPTIONS
  34.  
  35. #ifndef SLPRIMEM_H
  36. #include "SLPriMem.h"
  37. #endif
  38.  
  39. #ifndef FWPRIMEM_H
  40. #include "FWPriMem.h"
  41. #endif
  42.  
  43. #ifndef FWCLAINF_H
  44. #include "FWClaInf.h"
  45. #endif
  46.  
  47. #ifndef FWEXCIMP_H
  48. #include "FWExcImp.h"
  49. #endif
  50.  
  51. #include <stddef.h>
  52. #include <setjmp.h>
  53.  
  54. //========================================================================================
  55. // template <class T> FW_PrivStaticDestroyer(T*)
  56. // 
  57. // A template function that invokes the class T's destructor, using a static dispatch.
  58. // Even if the destructor for class T is virtual, a non-virtual function dispatch will be used.
  59. //========================================================================================
  60.  
  61. template <class T>
  62. inline void FW_PrivStaticDestroyer(T* self)
  63. {
  64.     self->T::~T();
  65. }
  66.  
  67. //========================================================================================
  68. // template <class T> FW_PrivClone(T*)
  69. // 
  70. // A template function that invokes the class T's copy constructor to clone an
  71. // instance of class T into a raw destination buffer
  72. //========================================================================================
  73.  
  74. template <class T>
  75. inline void FW_PrivClone(T* self, void* destination, size_t size)
  76. {
  77.     FW_PRIV_ASSERT(destination);
  78.     FW_PRIV_ASSERT(size >= sizeof(T));
  79.     new(destination) T(*self);
  80. }
  81.  
  82. //========================================================================================
  83. // class FW_CPrivWatcher
  84. //========================================================================================
  85.  
  86. class FW_CPrivWatcher : public FW_SPrivWatcher
  87. {
  88. public:
  89.     FW_CPrivWatcher(FW_PrivDeleteProc deleter)
  90.         { FW_PrivWatcher_Init(this, deleter); }
  91.     ~FW_CPrivWatcher()
  92.         { FW_PrivWatcher_Destroy(this); }
  93. };
  94.     
  95. //========================================================================================
  96. // CLASS FW_CPrivTryBlockContext
  97. //========================================================================================
  98.  
  99. class FW_CPrivTryBlockContext : public FW_SPrivTryBlockContext
  100. {
  101. public:
  102.     FW_CPrivTryBlockContext(jmp_buf buffer, FW_PrivLongJumpProc jumpProc)
  103.         { FW_PrivTryBlockContext_Init(this, buffer, jumpProc); }
  104.  
  105.     ~FW_CPrivTryBlockContext()
  106.         { FW_PrivTryBlockContext_Destroy(this); }
  107. };
  108.  
  109. //========================================================================================
  110. // template <class T> FW_PrivThrowException(const T& exception)
  111. //========================================================================================
  112.  
  113. template <class T>
  114. inline void FW_PrivThrowException(const T& exception)
  115. {
  116.     FW_PrivThrow((void*) &exception, 
  117.             sizeof(T),
  118.             exception.PrivVirtualGetClassInfo(), 
  119.             FW_PrivGetDestroyProc(&exception),
  120.             FW_PrivGetCloneProc(&exception));
  121. }
  122.  
  123. //========================================================================================
  124. // template <class T> FW_PrivGetAutoName(T*)
  125. //
  126. // We declare this function as a template function even though it will "specialized" for every instantiation via
  127. // the FW_DEFINE_AUTO macros.
  128. //========================================================================================
  129.  
  130. #ifdef FW_DEBUG
  131. template <class T>
  132. char* FW_PrivGetAutoName(const T*);
  133. #endif
  134.  
  135. //========================================================================================
  136. // template <class T> FW_PrivGetDestroyProc(T*)
  137. //
  138. // We declare this function as a template function even though it will "specialized" for every instantiation via
  139. // the FW_DEFINE_AUTO macros.
  140. //========================================================================================
  141.  
  142. template <class T>
  143. FW_PrivDestroyProc FW_PrivGetDestroyProc(const T*);
  144.  
  145. //========================================================================================
  146. // template <class T> FW_PrivGetDeleteProc(T*)
  147. //
  148. // We declare this function as a template function even though it will "specialized" for every instantiation via
  149. // the FW_DEFINE_AUTO macros.
  150. //========================================================================================
  151.  
  152. template <class T>
  153. FW_PrivDeleteProc FW_PrivGetDeleteProc(const T*);
  154.  
  155. #endif
  156.  
  157. #endif
  158.  
  159.